package C9_4_RemoveControlFlag;

/**
 * @author xugang1
 * @updated 2022/7/25
 * @description 移除控制标记
 */

public class RemoveControlFlag {

    /**
     * 重构思路1：break（continue）
     *
     * 重构思路2：return（解决未提供break和continue语句的编程语言中）
     *
     * tips：即使在支持break和continue语句的编程语言中,我通常也优先考虑思路2
     */


    /**
     * 重构前
     */
    public void test1(String[] people) {
        boolean found = false;
        for (int i = 0; i < people.length; i++) {
            if (!found) {
                if (people[i].equals("Don")) {
                    sendAlert();
                    found = true;
                }
                if (people[i].equals("John")) {
                    sendAlert();
                    found = true;
                }
            }
        }
    }

    /**
     * 重构后
     * 移除控制标记，转折处使用break
     */
    public void test2(String[] people) {
        for (int i = 0; i < people.length; i++) {
            if (people[i].equals("Don")) {
                sendAlert();
                break;
            }
            if (people[i].equals("John")) {
                sendAlert();
                break;
            }
        }
    }

    ====================================================================



    /**
     * 重构前
     */
    public void test3(String[] people) {
        boolean found = "";
        for (int i = 0; i < people.length; i++) {
            if (found.equals("")) {
                if (people[i].equals("Don")) {
                    sendAlert();
                    found = "Don";
                }
                if (people[i].equals("John")) {
                    sendAlert();
                    found = "John";//既是控制标记，也是运算结果
                }
            }
        }
    }

    /**
     * 重构后
     * 移除控制标记，转折处使用break
     * 1.found变量提炼到独立函数
     */
    public void test4(String[] people) {
        String found = foundMiscreant(people);
        someLaterCode(found)
    }

    String foundMiscreant(String[] people){
        boolean found = "";
        for (int i = 0; i < people.length; i++) {
            if (found.equals("")) {
                if (people[i].equals("Don")) {
                    sendAlert();
                    found = "Don";
                }
                if (people[i].equals("John")) {
                    sendAlert();
                    found = "John";//既是控制标记，也是运算结果
                }
            }
        }
        return found;
    }


    /**
     * 第二步重构后
     * 移除控制标记，转折处使用break
     * 2.return代替控制标记
     */
    public void test4(String[] people) {
        String found = foundMiscreant(people);
        someLaterCode(found)
    }

    String foundMiscreant(String[] people){
        boolean found = "";
        for (int i = 0; i < people.length; i++) {
            if (found.equals("")) {
                if (people[i].equals("Don")) {
                    sendAlert();
                    return "Don";
                }
                if (people[i].equals("John")) {
                    sendAlert();
                    return "John";
                }
            }
        }
        return "";
    }

}